package aex.action;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import aex.domain.Medicines;
import aex.service.ArticleCategoryService;
import aex.service.MedicinesService;
import aex.util.CU;
import aex.util.PageBean;

import com.opensymphony.xwork2.ModelDriven;

@Controller("MedicinesManagerAction")
@Scope("prototype")
public class MedicinesManagerAction extends BaseActon implements ModelDriven<Medicines> {

	private static final long serialVersionUID = 1L;


	@Resource(name = ArticleCategoryService.SERVICE_NAME)
	ArticleCategoryService acService;
	
	@Resource(name = MedicinesService.SERVICE_NAME)
	MedicinesService medicinesService;

	
	
	Medicines medicines = new Medicines();

	public MedicinesManagerAction() {
		CU.deBugPrintln("MedicinesAction 被创建");
	}

	@Override
	public Medicines getModel() {
		return medicines;
	}


	public String medicinesList(){
		String id=request.getParameter("id");
		PageBean<Medicines> pageBean = new PageBean<Medicines>();
		PageBean.initPageBean(pageBean, request,"&id="+id);
		pageBean = medicinesService.findAllByCategory(pageBean,id);
		request.setAttribute("pageBean", pageBean);
		request.setAttribute("category", acService.findByID(id));
		return "medicinesList";
	}
	

	/**
	 * 删除单个栏目
	 * @return
	 */
	public String deleteMedicines(){
		int id=Integer.parseInt(request.getParameter("id"));
		medicinesService.deleteById(id);
		ajaxPrint("删除成功！");
		return null;
	}
	
	/**
	 * 删除多个栏目
	 * @return
	 */
	public String deleteMediciness(){
		String[] ids=request.getParameter("ids").split(",");
		for (int i = 0; i < ids.length; i++) {
			medicinesService.deleteById(ids[i]);
		}
		ajaxPrint("成功删除"+ids.length+"条数据！");
		return null;
	}
	
	
	public String goAddMedicines(){
		String id=request.getParameter("id");
		request.setAttribute("category", acService.findByID(id));
		return "addMedicines";
	}
	
	public String addMedicines(){
		String id=request.getParameter("id");
		medicines.setArticleCategory(acService.findByID(id));
		medicines.setMeOrderBy(medicinesService.findMedicinesCount() + 1);
		medicines.setMeAvg((int)((medicines.getMeEffect()
									+medicines.getMePrice()
									+medicines.getMeSafety())/3));
		medicinesService.save(medicines);
		return CU.goMsgPage(request, "成功添加一个药物！", "ok", "manage/medicinesManager_medicinesList.do?id="+id);
	}
	public String editorMedicines(){
		request.setAttribute("medicines", medicinesService.findByID(request.getParameter("id")));
		request.setAttribute("category", acService.findByID(request.getParameter("acId")));
		return "editorMedicines";
	}
	
	public String updateMedicines(){
		String id=request.getParameter("acId");
		Medicines oldMedicines= medicinesService.findByID(medicines.getMeId());
		medicines.setMeOrderBy(oldMedicines.getMeOrderBy());
		medicines.setArticleCategory(oldMedicines.getArticleCategory());
		medicines.setMeAvg((int)((medicines.getMeEffect()
				+medicines.getMePrice()
				+medicines.getMeSafety())/3));
		medicinesService.update(medicines);
		
		return CU.goMsgPage(request, "药品信息已经更新！", "ok", "manage/medicinesManager_medicinesList.do?id="+id);
	}
	
	public String orderByMedicines(){
		String id=request.getParameter("id");
		request.setAttribute("seamCates", medicinesService.findAllByCategoryNoPage(id));
		request.setAttribute("id", id);
		return "orderByMedicines";
	}
	

	
	public String updateMedicinesOrderBy(){
		
		try{
			String[] seq=request.getParameter("cateSeq").split(",");
			//重新排序
			String id=request.getParameter("id");
			List<Medicines> seamCateList=medicinesService.findAllByCategoryNoPage(id);
			for (int i = 0; i < seq.length; i++) {
				int newSeq=Integer.parseInt(seq[i]);
				if(seamCateList.get(i).getMeOrderBy()!=newSeq){
					CU.deBugPrintln("更新"+seamCateList.get(i).getMeName()+" "+seamCateList.get(i).getMeOrderBy()+"---->"+newSeq);
					seamCateList.get(i).setMeOrderBy(newSeq);
					medicinesService.update(seamCateList.get(i));
				}
			}
			response.getWriter().write("{\"info\":\"修改成功！\",\"status\":\"y\"}");

		} catch (IOException e) {
			try {
				response.getWriter().write(
						"{\"info\":\"修改失败\",\"status\":\"y\"}");
			} catch (IOException e1) {
			}
		}
		return null;
	}
	
	


}
